//
//  WDLCycleView.m
//  06-CycleImages
//
//  Created by 王道林 on 16/7/21.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "WDLCycleView.h"

@interface WDLCycleView ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIScrollView *scrollView;

@end
@implementation WDLCycleView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubview:frame];
        [self initCycleScrollView];
        
    }
    return self;
}

#warning 后期需要添加page的位置改变, 这里默认位置
- (void)initCycleScrollView
{

    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.showsHorizontalScrollIndicator = NO;
}


- (void)creatSubview:(CGRect)frame
{
    // 子控件初始化时, frame不要与初始化方法的frame一样
    // 推荐使用 self.bounds   /  CGRectMake(0, 0, frame.size.width, frame.size.height)
    self.backgroundColor = [UIColor blackColor];
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:_scrollView];
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(frame.size.width / 3, frame.size.height - 40, frame.size.width / 3, 30)];
    _pageControl.center = CGPointMake(frame.size.width / 2, frame.size.height - 20);
    _pageControl.numberOfPages = 5;
    [self addSubview:_pageControl];
    
    [_pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    
}



// 自定义赋值方法
-(void)setImagesWithArray:(nonnull NSArray *)array
{
    // 判断类型(不知道别人使用你的类型时候,传进来的类型是什么
    id firstObject = [array firstObject];
    _imagesArray = [NSMutableArray array];
    // 判断对象是不是某种类型  iskingofClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把字符串数组 转化成 图片数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [_imagesArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]])
    {
        [_imagesArray setArray:array];
    }
    
    //添加前后照片
    UIImage *first = [_imagesArray firstObject];
    UIImage *last = [_imagesArray lastObject];
    [_imagesArray insertObject:last atIndex:0];
    [_imagesArray addObject:first];
    
    
    
    // 直接使用数组创建 Imageview    
    for (int i = 0; i < _imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0,self.bounds.size.width, self.bounds.size.height)];
        imageView.image = _imagesArray[i];
        [_scrollView addSubview:imageView];
    }
    
    _scrollView.contentSize = CGSizeMake(_imagesArray.count * self.bounds.size.width, 0);
    // 因为添加了最后一张图, 要让轮播图看起来在第一张
    _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    
}

// 协议
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _pageControl.currentPage = page - 1;
    if (page == 0) {
        _scrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArray.count - 2), 0);
        _pageControl.currentPage = _imagesArray.count - 3;
    } else if (page == _imagesArray.count - 1)
    {
        _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _pageControl.currentPage = 0;
    }
}
//
- (void)pageAction:(UIPageControl *)pageControl
{
    [_scrollView setContentOffset:CGPointMake((pageControl.currentPage + 1)* self.bounds.size.width, 0) animated:YES];
}



@end
